package com.hntest.controller;

import java.net.UnknownHostException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hntest.entity.IpLog;
import com.hntest.entity.LoginInfor;
import com.hntest.entity.Page;
import com.hntest.entity.SystemDictionary;
import com.hntest.entity.SystemDictionaryItem;
import com.hntest.service.IpLogService;
import com.hntest.service.LoginInforService;
import com.hntest.service.SystemDictionaryItemService;
import com.hntest.service.SystemDictionaryService;
import com.hntest.util.MD5;

@Controller(value = "HoutaiController")
public class HouTaiController {

	//登陆信息表业务类
	@Autowired
	private LoginInforService loginService;
	
	//数据字典目录业务类
	@Autowired
	private SystemDictionaryService dictionaryService;
	
	//数据字典明细业务类
	@Autowired
	private SystemDictionaryItemService dictionaryIteamSetvice;

	//登陆流水表业务类
	@Autowired
	private IpLogService iploginservice;
	
	public void setIploginservice(IpLogService iploginservice) {
		this.iploginservice = iploginservice;
	}

	public void setLoginService(LoginInforService loginService) {
		this.loginService = loginService;
	}
	
	//#####################	后台登陆	#############################
	
	//后台登陆
	@RequestMapping(value="/p2p/doLogin",method= {RequestMethod.POST})
	public String dologin(@RequestParam String userName,@RequestParam String passWD,ModelMap modelMap,HttpServletRequest request) {
		LoginInfor u = new LoginInfor();
		u.setUserName(userName);
		u.setPassWD(passWD);
		HttpSession session = request.getSession();
		LoginInfor user = loginService.doLogin(u);
		try {
			iploginservice.writLoginFlow(user);
		} catch (UnknownHostException e) {
			//本机ip有问题不允许登陆并且返回登陆
			return "forward:index.jsp?ip=no";
		}
		if(user!=null && user.getLoginType()!=0) {
			modelMap.put("userlogin", user);
			session.setAttribute("user", user);
			return "forward:/p2p/index.jsp";
		}else if(user!=null && user.getLoginType()==0) {
			return "forward:index.jsp?sp=no";
		}
		return "redirect:/p2p/login.jsp?sp=no";
	}
	
	//#####################	数据字典目录表	#############################
	
	//后台数据字典目录增添
	@RequestMapping(value="/p2p/addDictionary",method= {RequestMethod.POST})
	public String insertDictionary(@RequestParam String title,@RequestParam String id,ModelMap modelMap) {
		int n = dictionaryService.addDictionary(id, title);
		Page page = new Page();
		 //计算总记录数
		 page.setTotalCounts(dictionaryService.getCount());
		 //查询所有的记录数
		 List<SystemDictionary> list = dictionaryService.getListByPage(page,null);
		 modelMap.put("list", list);
		 modelMap.put("page", page);
		 if(n>0) {
			 return "forward:/p2p/dictionary.jsp?sp=insertOk";
		 }
		return "forward:/p2p/dictionary.jsp?sp=insertNo";
	}
	
	//查询所有的数据字典目录
	@RequestMapping(value="/p2p/toDictionary",method= {RequestMethod.POST,RequestMethod.GET})
	public String toDictionary(ModelMap modelMap) {
		List<SystemDictionary> list = dictionaryService.getAll();
		modelMap.put("list", list);
		return "forward:/p2p/dictionary.jsp";
	}
	
	//初始化数据字典目录
	@RequestMapping(value="/p2p/toDictionary2",method= {RequestMethod.POST,RequestMethod.GET})
	public String toDictionary2(ModelMap modelMap) {
		 Page page = new Page();
		 //计算总记录数
		 page.setTotalCounts(dictionaryService.getCount());
		 //查询所有的记录数
		 List<SystemDictionary> list = dictionaryService.getListByPage(page,null);
		 modelMap.put("list", list);
		 modelMap.put("page", page);
		 return "forward:/p2p/dictionary.jsp";
	}
	
	//按照分页查询数据字典目录查询
	@RequestMapping(value="/p2p/selectDictionary",method= {RequestMethod.POST,RequestMethod.GET})
	public String selectDictionary2(@RequestParam String sn,Page page,ModelMap modelMap) {
		SystemDictionary a = new SystemDictionary();
		a.setSn(sn);
		//按照分页查询记录
		List<SystemDictionary> list = dictionaryService.getListByPage(page,a);
		//计算总记录数
		 page.setTotalCounts(dictionaryService.getCount());
		 modelMap.put("list", list);
		 modelMap.put("page", page);
		 return "forward:/p2p/dictionary.jsp";
	}
	
	//删除数据字典目录信息对象
	@RequestMapping(value="/p2p/deletDictionary",method= {RequestMethod.POST,RequestMethod.GET})
	public String deleteDictionary(@RequestParam int id,ModelMap modelMap) {
		int n = dictionaryService.deleteDictionary(id);
		Page page = new Page();
		 //计算总记录数
		 page.setTotalCounts(dictionaryService.getCount());
		 //查询所有的记录数
		 List<SystemDictionary> list = dictionaryService.getListByPage(page,null);
		 modelMap.put("list", list);
		 modelMap.put("page", page);
		if(n>0) {
			return "forward:/p2p/dictionary.jsp?sp=deleteOk";
		}
		
		return "forward:/p2p/dictionary.jsp?sp=deleteNo";
	}
	
	//#####################	数据字典明细表	#############################
	
	//初始化查询数据字典明细
	@RequestMapping(value="/p2p/toDictionaryItem",method= {RequestMethod.POST,RequestMethod.GET})
	public String toDictionaryItem(ModelMap modelMap) {
		Page page = new Page();
		page.setTotalCounts(dictionaryIteamSetvice.getCount());
		List<SystemDictionary> dictionary = dictionaryService.getAll();
		List<SystemDictionaryItem> list = dictionaryIteamSetvice.getListByPage(page, null);
		modelMap.put("list", list);
		modelMap.put("page", page);
		modelMap.put("dictionary", dictionary);
		return "forward:/p2p/dictionaryItem.jsp";
	}
	
	//条件查询数据字典明细
	@RequestMapping(value="/p2p/selectDictionaryItem",method= {RequestMethod.POST,RequestMethod.GET})
	public String toDictionaryItem(@RequestParam String title,@RequestParam int dic,Page page,ModelMap modelMap) {
		SystemDictionaryItem dictionaryitem = new SystemDictionaryItem();
		SystemDictionary parent = new SystemDictionary();
		parent.setDictionaryId(dic);
		dictionaryitem.setItemTitle(title);
		dictionaryitem.setParent(parent);
		//再次初始化查询
		//page.setTotalCounts(dictionaryIteamSetvice.getCount());
		page.setTotalCounts(dictionaryIteamSetvice.getConditionCount(dictionaryitem));
		List<SystemDictionary> dictionary = dictionaryService.getAll();
		List<SystemDictionaryItem> list = dictionaryIteamSetvice.getListByPage(page, dictionaryitem);
		modelMap.put("list", list);
		modelMap.put("page", page);
		modelMap.put("dictionary", dictionary);
		//搜索条件传回去
		modelMap.put("title", title);
		modelMap.put("dic", dic);
		return "forward:/p2p/dictionaryItem.jsp";
	}
	
	//删除数据字典明细记录
	@RequestMapping(value="/p2p/deleteDictionaryItem",method= {RequestMethod.POST,RequestMethod.GET})
	public String deleteDictionaryItem(@RequestParam int itemId,ModelMap modelMap) {
		int n = dictionaryIteamSetvice.deleteItem(itemId);
		Page page = new Page();
		page.setTotalCounts(dictionaryIteamSetvice.getCount());
		List<SystemDictionary> dictionary = dictionaryService.getAll();
		List<SystemDictionaryItem> list = dictionaryIteamSetvice.getListByPage(page, null);
		modelMap.put("list", list);
		modelMap.put("page", page);
		modelMap.put("dictionary", dictionary);
		if(n>0) {
			return "forward:/p2p/dictionaryItem.jsp?sp=deleteOk";
		}
		return "forward:/p2p/dictionaryItem.jsp?sp=deleteNo";
	} 
	
	//新增数据字典明细
	@RequestMapping(value="/p2p/insertDictionaryItem",method= {RequestMethod.POST,RequestMethod.GET})
	public String insertDictionaryItem(@RequestParam String title,@RequestParam int dic2,ModelMap modelMap) {
		int n = dictionaryIteamSetvice.insertItem(title, dic2);
		Page page = new Page();
		page.setTotalCounts(dictionaryIteamSetvice.getCount());
		List<SystemDictionary> dictionary = dictionaryService.getAll();
		List<SystemDictionaryItem> list = dictionaryIteamSetvice.getListByPage(page, null);
		modelMap.put("list", list);
		modelMap.put("page", page);
		modelMap.put("dictionary", dictionary);
		if(n>0) {
			return "forward:/p2p/dictionaryItem.jsp?sp=insertOk";
		}
		return "forward:/p2p/dictionaryItem.jsp?sp=insertNo";
	}
	//#####################	登陆流水表	#############################
	//初始化登陆流水信息(有错误，估计不能拿到dao类)
	/*public static void IntIpLog(ModelMap modelMap){
		HouTaiController h = new HouTaiController();
		Page page = new Page();
		List<IpLog> list = h.iploginservice.getListByPage(page, null);
		page.setTotalCounts(h.iploginservice.getCount());
		modelMap.put("page", page);
		modelMap.put("list", list);
	}*/
	
	//初始化登陆流水表
	@RequestMapping(value="/p2p/toIpLog",method= {RequestMethod.POST,RequestMethod.GET})
	public String toIpLog(ModelMap modelMap) {
		//初始化登录流水
		Page page = new Page();
		List<IpLog> list = iploginservice.getListByPage(page, null);
		page.setTotalCounts(iploginservice.getCount());
		modelMap.put("page", page);
		modelMap.put("list", list);
		return "forward:/p2p/IpLog.jsp";
	}
	
	//查询登陆流水表
	@RequestMapping(value="/p2p/selectIpLog",method= {RequestMethod.POST,RequestMethod.GET})
	public String selectIpLog(@RequestParam String userName,@RequestParam int type,Page page,ModelMap modelMap) {
		LoginInfor l = new LoginInfor();
		l.setUserName(userName);
		l.setLoginType(type);
		page.setTotalCounts(iploginservice.getConditionCount(l));
		List<IpLog> list = iploginservice.getListByPage(page, l);
		//再次初始化查询
		modelMap.put("page", page);
		modelMap.put("list", list);
		//将查询条件传回去
		modelMap.put("check", type);
		modelMap.put("userName", userName);
		return "forward:/p2p/IpLog.jsp";
	}
	
	//#####################	登陆流水表	#############################
}

